package com.ruoyi.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.domain.FormSetting;
import com.ruoyi.manage.domain.FormSettingBig;
import com.ruoyi.manage.domain.SurgeryFormDetail;
import com.ruoyi.manage.service.IFormSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.SurgeryFormMapper;
import com.ruoyi.manage.domain.SurgeryForm;
import com.ruoyi.manage.service.ISurgeryFormService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 手术单关联Service业务层处理
 * 
 * @author zww
 * @date 2025-07-14
 */
@Service
public class SurgeryFormServiceImpl implements ISurgeryFormService 
{
    @Autowired
    private SurgeryFormMapper surgeryFormMapper;
    @Autowired
    private IFormSettingService formSettingService;
    /**
     * 查询手术单关联
     * 
     * @param surgeryFormId 手术单关联主键
     * @return 手术单关联
     */
    @Override
    public SurgeryForm selectSurgeryFormBySurgeryFormId(Long surgeryFormId)
    {
        return surgeryFormMapper.selectSurgeryFormBySurgeryFormId(surgeryFormId);
    }

    /**
     * 查询手术单关联列表
     * 
     * @param surgeryForm 手术单关联
     * @return 手术单关联
     */
    @Override
    public List<SurgeryForm> selectSurgeryFormList(SurgeryForm surgeryForm)
    {
        return surgeryFormMapper.selectSurgeryFormList(surgeryForm);
    }

    /**
     * 新增手术单关联
     * 
     * @param surgeryForm 手术单关联
     * @return 结果
     */
    @Override
    public int insertSurgeryForm(SurgeryForm surgeryForm)
    {
        insertSurgeryFormDetail(surgeryForm);
        return surgeryFormMapper.insertSurgeryForm(surgeryForm);
    }

    private void insertSurgeryFormDetail(SurgeryForm surgeryForm) {
        List<SurgeryFormDetail> SurgeryFormDetailList = surgeryForm.getSurgeryFormDetailList();
        Long surgeryId = surgeryForm.getSurgeryId();
        Long surgeryFormId = surgeryForm.getSurgeryFormId();
        if (StringUtils.isNotNull(SurgeryFormDetailList)) {
            List<SurgeryFormDetail> list = new ArrayList<>();
            for (SurgeryFormDetail surgeryFormDetail : SurgeryFormDetailList) {
                surgeryFormDetail.setSurgeryId(surgeryId);
                surgeryFormDetail.setSurgeryFormId(surgeryFormId);
                list.add(surgeryFormDetail);
            }
            if (!list.isEmpty()) {
                surgeryFormMapper.batchSurgeryFormDetail(list);
            }
        }
    }

    /**
     * 修改手术单关联
     * 
     * @param surgeryForm 手术单关联
     * @return 结果
     */
    @Override
    public int updateSurgeryForm(SurgeryForm surgeryForm)
    {
        surgeryFormMapper.deleteSurgeryFormDetailBySurgeryFormId(surgeryForm.getSurgeryFormId());
        insertSurgeryFormDetail(surgeryForm);
        return surgeryFormMapper.updateSurgeryForm(surgeryForm);
    }

    /**
     * 批量删除手术单关联
     * 
     * @param surgeryFormIds 需要删除的手术单关联主键
     * @return 结果
     */
    @Override
    public int deleteSurgeryFormBySurgeryFormIds(Long[] surgeryFormIds)
    {
        return surgeryFormMapper.deleteSurgeryFormBySurgeryFormIds(surgeryFormIds);
    }

    /**
     * 删除手术单关联信息
     * 
     * @param surgeryFormId 手术单关联主键
     * @return 结果
     */
    @Override
    public int deleteSurgeryFormBySurgeryFormId(Long surgeryFormId)
    {
        return surgeryFormMapper.deleteSurgeryFormBySurgeryFormId(surgeryFormId);
    }

}
